<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/25
 * Time: 15:47
 */

namespace Admin\Controller;
use Admin\Model\UserModel;
use Think\Controller;

class SystemController extends BaseController {

    //系统管理
    public function system_manage()
    {
        $nickname = I('post.nickname');
        if(!empty($nickname))
        {
            $condition['user'] = $nickname;
        }
        $model = D('admin');
        $count = $model->where($condition)->count();
        $Page       = new \Think\Page($count,8);// 实例化分页类
        $show       = $Page->show();// 分页显示输出
        $list = $model->where($condition)->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('data',$list);// 赋值数据集$this->assign('page',$show);// 赋值分页输出
        $this->assign('page',$show);// 赋值分页输出
       $this->display();
    }

    //发送系统消息
    public function system_message()
    {
        $this->display();
    }

    //添加用户
    public function add_user()
    {
        $this->display();
    }

    //将添加的用户写入数据库
    public function write_add_user()
    {
        $model = D('admin');
        $data = I('post.');
        $data['password'] = md5($data['password']);
        $res = $model->data($data)->add();
        if(!empty($res))
        {
            redirect(U('admin.php/system/system_manage'));
        }
//        var_dump($data);
    }

    //更改用户
    public function update_user()
    {
        $id = I('get.id');
        $condition['id'] = $id;
        $model = D('admin');
        $data = $model->where($condition)->select();
        $this->assign('data',$data[0]);
        $this->display();
    }

    //将更改信息写入数据库
    public function write_update_user()
    {
        $model = D('admin');
        $id = I('post.id');
        $status = I('post.status');
        $condition['id'] = $id;
        $data['status'] = $status;
        $model->where($condition)->save($data);
        redirect(U('system/system_manage'));
    }

    //重置密码
    public function reset_password()
    {
        $model = D('admin');
        $id = I('get.id');
        $condition['id'] = $id;
        $data = $model->where($condition)->select();
        $this->assign('data',$data[0]);
        $this->display();
    }

    //更改密码
    public function write_reset_password()
    {
        $model = D('admin');
        $id = I('post.id');
        $old_password = md5(I('post.old_password'));
        $new_password = md5(I('post.new_password'));
        $condition['id'] = $id;
        $condition ['password'] = $old_password;
        $res = $model->where($condition)->select();
        if(!empty($res))
        {
            $conditions['id'] = $id;
            $data['password'] = $new_password;
            $model->where($conditions)->save($data);
           $result = array(
               'msg'=>'success',
               'status'=>1
           );
        }else{
            $result = array(
                'msg'=>'failed',
                'status'=>2
            );
        }
        $this->ajaxReturn($result);
    }

    //用户反馈
    public function user_feedback()
    {
        $this->display();
    }

    //详细信息
    public function user_detail()
    {
        $this->display();
    }

    //生成认证码
    public function auth_code()
    {
        $len =6;
         $chars_array = array(
                "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
                "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
                "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
                "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
                "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
                "S", "T", "U", "V", "W", "X", "Y", "Z",
            );
            $charsLen = count($chars_array) - 1;

            $outputstr = "";
            for ($i=0; $i<$len; $i++)
            {
                $outputstr .= $chars_array[mt_rand(0, $charsLen)];
            }

        //将生成的认证码写入数据库
        $model = new UserModel();
        $model->write_auth_code($outputstr);
        $this->ajaxReturn($outputstr);
    }
}